package com.tyjc.unique.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.tyjc.unique.util.ScreenUtils;


@SuppressLint("NewApi")
public abstract class ZouShiOnDraw extends LinearLayout{
	
	

	protected Context context;
	protected int kj[][];
	protected int screen_width, screen_height, screen_DPI;
	protected float screen_density;
	protected static final float count = 13;//一屏幕可布置小球的个数（以此求单位宽度）
	protected float unitLength = 10;//单位宽度（高度）
	protected int column = 33;//列数
	protected int row = 30;//行数
	
	protected float totalWidth = 100;//画布总宽度
	protected float totalHeight = 100;//画布总高度
	
	public boolean isShowTongJi = false;
	public boolean isShowZheXian = false;
	public boolean isShowYiLou = true;

	public ZouShiOnDraw(Context context) {
		super(context);
		init(context);
	}
	
	public ZouShiOnDraw(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	public ZouShiOnDraw(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	protected void init(Context context){
		setWillNotDraw(false);
		if (isInEditMode()) {
			return;
		}
		this.context = context;
		
		this.screen_width = ScreenUtils.getInstance().getWidth();
		this.screen_height = ScreenUtils.getInstance().getHeight();
		this.screen_DPI = ScreenUtils.getInstance().getDensityDPI();
		this.screen_density = ScreenUtils.getInstance().getDensity();
//		this.screen_height = 500;
//		this.screen_width = 400;
		
		
		unitLength = screen_width / count;
		totalWidth = screen_width;
		totalHeight = screen_height;
		
		setFocusable(true);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int width = (int)this.totalWidth;
		int height = (int)this.totalHeight;
		setMeasuredDimension(width, height);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}
	
	/**
	 * 绘制文字
	 * @param canvas
	 * @param paint
	 * @param text
	 * @param rect
	 */
	protected void drawText(Canvas canvas,Paint paint,String text,Rect rect){
		FontMetrics fm = paint.getFontMetrics();
		int baseLine = (int)(rect.top + (rect.bottom - rect.top - fm.bottom + fm.top) / 2 - fm.top);  
		canvas.drawText(text, rect.centerX() - (paint.measureText(text)/2), baseLine, paint);
	}
	
	public int getTotalWidth(){
		return (int)totalWidth;
	}
	
	public int getTotalHeight(){
		return (int)totalHeight;
	}

	public void setInfors(int kjj[][]){
		this.kj = kjj;
		totalWidth = screen_width;
		totalHeight = screen_height;
		if (kj != null&& kj.length != 0) {
			this.row = kj.length;
			if (totalHeight < unitLength * row) {
				totalHeight = unitLength * row;
			}
			
			if (kj[0].length != 0
//					&& totalWidth < unitLength * kj[0].length
					) {
				this.column = kj[0].length;
				totalWidth = unitLength * kj[0].length;
			}
			
//			String str = "";
//			for (int i = 0; i < kj.length; i++) {
//				for (int j = 0; j < kj[i].length; j++) {
//					str += kj[i][j] + " ";
//				}
//				str += "\n";
//			}
//			Log.e("----------------", ""+str);
		}else{
		}
		
		setTotalWH();

	}
	
	protected abstract void setTotalWH();
}
